define('common:widget/ui/ai/toast/toast', function(require, exports, module) {

  /**
   * @file 弹窗提醒
   * @author yuanmin
   */
  var ToastComponents = require('common:widget/ui/ai/toast/toastComponents');
  // 每个 toast 定义优先级，决定是否替换正在显示的 toast
  var priorityMap = {
      low: 10, // 默认优先级
      middle: 20,
      high: 30
  };
  
  function NewToast(options) {
      // 调用父类构造函数
      ToastComponents.call(this);
  
      // 从 options 中解构属性
      var message = options.message;
      var duration = options.duration || 3000;
  
      // 初始化属性
      this.toastID = new Date().getTime();
      this.data.set('message', message);
      this.data.set('duration', duration);
  }
  // 设置 NewToast 的原型以继承 ToastComponents 的方法
  NewToast.prototype = Object.create(ToastComponents.prototype);
  NewToast.prototype.constructor = NewToast;
  
  // 添加 NewToast 静态属性
  NewToast.toastInstances = [];
  NewToast.defaultPriority = 10;
  NewToast.currentPriority = -1;
  
  function sendToast(options, priority = 'low', mountDom = 'ai-img-container') {
      var toast = new NewToast(options);
      // 挂载 toast 到编辑页面上
      var currentPriority = priorityMap[priority] || NewToast.defaultPriority;
  
      // 新的 toast 优先级大于等于当前的 toast 优先级，才展示
      if (currentPriority >= NewToast.currentPriority) {
  
          // 更新当前展示的优先级
          NewToast.currentPriority = currentPriority;
  
          // 已有 toast 实例全部卸载
          NewToast.toastInstances.forEach(function (toast) {
              toast.detach();
          });
          NewToast.toastInstances = [];
          NewToast.toastInstances.push(toast); // 实例数组中只会同时存在一个 toast
  
          // 挂载 toast 到页面上
          var app = document.getElementById(mountDom);
          toast.attach(app);
      }
      setTimeout(function () {
          // 在数组实例里面才能触发 detach。否则是已经卸载了的
          NewToast.toastInstances.forEach(function (item) {
              if (item.toastID === toast.toastID) {
                  toast.detach();
                  NewToast.toastInstances = [];
                  NewToast.currentPriority = -1; // 重置 toast 优先级
              }
          });
      }, toast.data.get('duration'));
  }
  
  module.exports = sendToast;
  

});
